package com.imooc.controller;

import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.pojo.vo.ItemInfoVO;
import com.imooc.pojo.vo.ShopcartVO;
import com.imooc.service.ItemService;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.imooc.controller.BaseController.*;

/**
 * @author Administrator
 */
@Api(value = "用于商品详情接口管理", tags = {"用于商品详情接口管理"})
@RestController
@RequestMapping({"/item/info"})
public class ItemController {

    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("get-items/{itemId}")
    public IMOOCJSONResult getNewSixItems(@ApiParam(name = "itemId", value = "商品id", required = true)
                                          @PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return IMOOCJSONResult.errorMsg("商品id不能为空");
        }
        Items items = itemService.getById(itemId);
        ItemsParam itemsParam = itemService.getByItemId(itemId);
        List<ItemsImg> itemsImgList = itemService.getItemsImgListByItemId(itemId);
        List<ItemsSpec> itemsSpecList = itemService.getItemsSpecsByItemId(itemId);
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemParams(itemsParam);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        return IMOOCJSONResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("get-comment-count")
    public IMOOCJSONResult getCommentCount(@ApiParam(name = "itemId", value = "商品id", required = true)
                                           @RequestParam String itemId) {
        if (null == itemId) {
            return IMOOCJSONResult.errorMsg("商品id不能为空");
        }
        return IMOOCJSONResult.ok(this.itemService.getCommentCount(itemId));
    }

    @ApiOperation(value = "评论等级分页", notes = "评论等级分页", httpMethod = "GET")
    @GetMapping("list-page-items-comments")
    public IMOOCJSONResult listPageItemsComments(@ApiParam(name = "itemId", value = "商品id", required = true) @RequestParam String itemId,
                                                 @ApiParam(name = "commentLevel", value = "评论等级", required = true) @RequestParam Integer commentLevel,
                                                 @ApiParam(name = "page", value = "第几页") @RequestParam Integer page,
                                                 @ApiParam(name = "pageSize", value = "该页多少页数") @RequestParam Integer pageSize) {
        if (null == itemId) {
            return IMOOCJSONResult.errorMsg("商品id不能为空");
        }
        if (null == commentLevel) {
            return IMOOCJSONResult.errorMsg("评论等级不能为空");
        }

        if (null == page) {
            page = PAGE;
        }

        if (null == pageSize) {
            pageSize = COMMON_PAGE_SIZE;
        }

        return IMOOCJSONResult.ok(itemService.listPageItemsComments(itemId, commentLevel, page, pageSize));
    }

    @ApiOperation(value = "商品分页数据", notes = "商品分页数据", httpMethod = "GET")
    @GetMapping("search-list-item")
    public IMOOCJSONResult searchListPage(@ApiParam(name = "keywords", value = "关键字", required = true) @RequestParam String keywords,
                                          @ApiParam(name = "sort", value = "排序字段", required = true) @RequestParam String sort,
                                          @ApiParam(name = "page", value = "第几页") @RequestParam Integer page,
                                          @ApiParam(name = "pageSize", value = "该页多少页数") @RequestParam Integer pageSize) {

        if (null == page) {
            page = PAGE;
        }

        if (null == pageSize) {
            pageSize = PAGE_SIZE;
        }

        return IMOOCJSONResult.ok(itemService.searchListPageItems(keywords, sort, page, pageSize));
    }

    @ApiOperation(value = "通过分类id获取商品数据", notes = "通过分类id获取商品数据", httpMethod = "GET")
    @GetMapping("search-list-page-by-cat-id")
    public IMOOCJSONResult searchListPageByCatId(@ApiParam(name = "catId", value = "关联分类id", required = true) @RequestParam Integer catId,
                                                 @ApiParam(name = "sort", value = "排序字段", required = true) @RequestParam String sort,
                                                 @ApiParam(name = "page", value = "第几页") @RequestParam Integer page,
                                                 @ApiParam(name = "pageSize", value = "该页多少页数") @RequestParam Integer pageSize) {

        if (null == page) {
            page = PAGE;
        }

        if (null == pageSize) {
            pageSize = PAGE_SIZE;
        }

        return IMOOCJSONResult.ok(itemService.searchListPageByCatId(catId, sort, page, pageSize));
    }

    @ApiOperation(value = "根据商品规格ids查找最新的商品数据", notes = "用于用户长时间未登录网站，刷新购物车中的数据（主要是商品价格），类似京东淘宝", httpMethod = "GET")
    @GetMapping("/refresh")
    public IMOOCJSONResult refresh(
            @ApiParam(name = "itemSpecIds", value = "拼接的规格ids", required = true, example = "1001,1003,1005")
            @RequestParam String itemSpecIds) {

        if (StringUtils.isBlank(itemSpecIds)) {
            return IMOOCJSONResult.ok();
        }

        List<ShopcartVO> list = itemService.queryItemsBySpecIds(itemSpecIds);

        return IMOOCJSONResult.ok(list);
    }

}